package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.system.Syslog;
import com.itheima.mapper.system.SyslogMapper;
import com.itheima.service.system.SyslogService;
import com.itheima.utils.MapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * service层，处理数据
 */
public class SyslogServiceImpl implements SyslogService {
    //查询所有
    @Override
    public List<Syslog> findAll(String pageNum, String pageSize) {
        //设置默认分页信息
        Integer pageN = 1;
        Integer pageS = 5;
        //判断前端是否传入数据，重新给pageN,pageS赋值
        if(StringUtils.isNotBlank(pageNum)){
            pageN = Integer.parseInt(pageNum);

        }
        if(StringUtils.isNotBlank(pageSize)){
            pageS = Integer.parseInt(pageSize);
        }
        SyslogMapper mapper = MapperUtil.getMapper(SyslogMapper.class);
        //分页插件设置值
        PageHelper.startPage(pageN,pageS);
        return mapper.findAll();
    }

    //删除功能
    @Override
    public void deleteById(String id) {
        //id: 1,2,3,4  切割字符串，遍历删除数据
        String[] split = id.split(",");
        for (String s : split) {
            SyslogMapper mapper = MapperUtil.getMapper(SyslogMapper.class);
            mapper.deleteById(s);
        }
    }

    //导出excel
    @Override
    public ByteArrayOutputStream findExport() {
        SyslogMapper mapper = MapperUtil.getMapper(SyslogMapper.class);
        //查询所有数据
        List<Syslog> syslogList = mapper.findAll();
        //获取到对应的Excel文件，工作簿文件
        Workbook wb = new XSSFWorkbook();
        //创建工作表
        Sheet s = wb.createSheet("访问日志");

        //设置通用配置
        CellStyle cs_field = wb.createCellStyle();
        cs_field.setAlignment(HorizontalAlignment.CENTER);
        cs_field.setBorderTop(BorderStyle.THIN);
        cs_field.setBorderBottom(BorderStyle.THIN);
        cs_field.setBorderLeft(BorderStyle.THIN);
        cs_field.setBorderRight(BorderStyle.THIN);

        //制作标题占的列数
        s.addMergedRegion(new CellRangeAddress(1,1,1,7));

        Row row_1 = s.createRow(1);
        Cell cell_1_1 = row_1.createCell(1);
        cell_1_1.setCellValue("访问日志");
        //创建一个样式
        CellStyle cs_title = wb.createCellStyle();
        cs_title.setAlignment(HorizontalAlignment.CENTER);
        cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
        cell_1_1.setCellStyle(cs_title);

        //制作表头
        String[] fields = {"ID","访问时间","访问用户","访问IP","资源URL", "执行时间","访问方法"};
        Row row_2 = s.createRow(2);

        for (int i = 0; i < fields.length; i++) {
            Cell cell_2_temp = row_2.createCell(1 + i); //++
            cell_2_temp.setCellValue(fields[i]);    //++
            cell_2_temp.setCellStyle(cs_field);
        }

        //制作数据区
        int row_index = 0;
        for (Syslog q : syslogList) {
            int cell_index = 0;
            Row row_temp = s.createRow(3 + row_index++);

            Cell cell_data_1 = row_temp.createCell(1 + cell_index++);
            cell_data_1.setCellValue(q.getId());    //++
            cell_data_1.setCellStyle(cs_field);

            Cell cell_data_2 = row_temp.createCell(1 + cell_index++);
            cell_data_2.setCellValue(q.getVisitTime());    //++
            cell_data_2.setCellStyle(cs_field);

            Cell cell_data_3 = row_temp.createCell(1 + cell_index++);
            cell_data_3.setCellValue(q.getUsername());    //++
            cell_data_3.setCellStyle(cs_field);

            Cell cell_data_4 = row_temp.createCell(1 + cell_index++);
            cell_data_4.setCellValue(q.getIp());    //++
            cell_data_4.setCellStyle(cs_field);

            Cell cell_data_5 = row_temp.createCell(1 + cell_index++);
            cell_data_5.setCellValue(q.getUrl());    //++
            cell_data_5.setCellStyle(cs_field);

            Cell cell_data_6 = row_temp.createCell(1 + cell_index++);
            cell_data_6.setCellValue(q.getExecutionTime());    //++
            cell_data_6.setCellStyle(cs_field);

            Cell cell_data_7 = row_temp.createCell(1 + cell_index++);
            cell_data_7.setCellValue(q.getMethod());    //++
            cell_data_7.setCellStyle(cs_field);


        }
        //创建输出流对象
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返还数据
        return os;
    }

    @Override
    public List<Syslog> findLike(String pageNum, String pageSize, String name) {
        //设置默认分页信息
        Integer pageN = 1;
        Integer pageS = 5;
        //判断前端是否传入数据，重新给pageN,pageS赋值
        if(StringUtils.isNotBlank(pageNum)){
            pageN = Integer.parseInt(pageNum);

        }
        if(StringUtils.isNotBlank(pageSize)){
            pageS = Integer.parseInt(pageSize);
        }
        SyslogMapper mapper = MapperUtil.getMapper(SyslogMapper.class);
        //分页插件设置值
        PageHelper.startPage(pageN,pageS);
        return mapper.findLike(name);
    }

    //获取日志信息
    @Override
    public void setLog(String url, String methodName, String remoteAddr,long start) {
       // System.out.println(url + "===" + methodName + "===" + remoteAddr);
        //获取方法执行完的时间
        long end = System.currentTimeMillis();
        //获取执行时间
        int time = (int) (end - start);
        String id =  getId();
        SyslogMapper mapper = MapperUtil.getMapper(SyslogMapper.class);
        Syslog syslog = new Syslog();
        syslog.setId(id);
        syslog.setVisitTime(new Date());
        syslog.setUsername("666");
        syslog.setIp(remoteAddr);
        syslog.setUrl(url);
        syslog.setExecutionTime(time);
        syslog.setMethod(methodName);
        mapper.setLog(syslog);
    }

    //根据数据库获取id做自增
    private  String getId(){
        SyslogMapper mapper = MapperUtil.getMapper(SyslogMapper.class);
        //获取所有的id
        List<String> list  = mapper.getId();
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ids.add(Integer.parseInt(list.get(i)));
        }
        //排序
        Collections.sort(ids);
       Integer max =  ids.get(list.size()-1);
       //实现自增
       return max +  1 + "";
    }
}
